/**
 * 
 */

function getDistrict() {
	var city = $('#city').val();
	$('#district option[value!="0"]').remove();
	if (city != 0) {
		$.ajax({
			"type" : "GET",
			"async" : false,
			"contentType" : "application/json;charset=utf-8",
			"dataType" : "json",
			"url" : "/core-web-admin/admin/getdistrictbycityid",
			"data" : {
				"cityId" : $('#city').val(),
			},
			"success" : function(data) {
//				$.each(data, function(index, value) {
//					$('#district').append(
//							$('<option>').text(value).attr(
//									'value', index));
//				});
				$.each(data, function(index, value) {
					$('#districtId').append(
							$('<option>').text(value.districtName).attr(
									'value', value.districtId));
				});
			},
			"error" : function(data) {
				alert("ERROR WHEN GET DISTRICT!");
				flag = false;
			}
		});
	}
}

function validateForm() {
	var error = document.getElementById("error");
	while (error.hasChildNodes()) {
		error.removeChild(error.childNodes[0]);
	}
	var name = document.getElementById("adminName").value;
	var email = document.getElementById("adminEmail").value;
	var username = document.getElementById("adminLoginId").value;
	var password = document.getElementById("adminPassword").value;
	var role = document.getElementById("roleId").value;
	var check = true;
	var textError = null;
	if (name == null || name == "") {
		textError = "Admin Name must be filled out!";
		addElement(error, textError);
		check = false;
	}
	if (email == null || email == "") {
		textError = "Email must be filled out!";
		addElement(error, textError);
		check = false;
	} else if (checkExistedEmail()) {
		textError = "Email has existed!";
		addElement(error, textError);
		check = false;
	}
	if (username == null || username == "") {
		textError = "Username must be filled out!";
		addElement(error, textError);
		check = false;
	} else if (checkExistedUsername()) {
		textError = "Admin has existed!";
		addElement(error, textError);
		check = false;
	}
	if (password == null || password == "") {
		textError = "Password must be filled out!";
		addElement(error, textError);
		check = false;
	}
	var birthday = document.getElementById("adminBirthday").value;
	if (birthday != null && birthday != "") {
		var adminBirthday = new Date(birthday);
		var today = new Date();
		if (today > adminBirthday) {
			textError = "Birthday is invalid!";
			addElement(error, textError);
			check = false;
		}
	}
	if (role == -1 || role == "" || role == null) {
		textError = "Please choose a role!";
		addElement(error, textError);
		check = false;
	}
	return check;
}

function addElement(div, textError) {

	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);

	var text = document.createTextNode(textError);

	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedUsername() {
	var flag = false;
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/admin/checkexistedadminloginid",
		"data" : {
			"adminLoginId" : $('#adminLoginId').val()
		},
		"success" : function(data) {
			if (data == true) {
				flag = true;
			}
			if (data == false) {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED USER NAME!");
			flag = false;
		}
	});
	return flag;
}

function checkExistedEmail() {
	var flag = false;
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/admin/checkexistedemail",
		"data" : {
			"adminEmail" : $('#adminEmail').val()
		},
		"success" : function(data) {
			if (data == true) {
				flag = true;
			}
			if (data == false) {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED EMAIL!");
			flag = false;
		}
	});
	return flag;
}